 
*******************
 	cd "D:\Dropbox\book_welfare\replication"

use  data\bookstat_2021_welfare_asin.dta, clear   


keep if pyear==2021 


gen dfemale = mshare~=. & mshare<.5 
gen dmale = mshare~=. & mshare>.5 
gen dunk = mshare==. 

gen gender = dfemale + 2*dunk 

gen g = 0 if dmale==1 
replace g= 1 if dfemale==1 
replace g = 2 if dunk==1 

gsort gender -q

bysort gender: gen rank = _n 
bysort gender: gen N = _N 

preserve 
	collapse (count) h=year, by(gender)
	gen index=1
	reshape wide h, i(index) j(gender)
	gen ratio = h1/(h1 +  h0)
	su ratio 
restore


preserve 
	clear 
	set obs 1 
	gen x = .
	save data\simulated_sf_nf.dta, replace 
	
restore 


forvalues k=0(1) 100 {
	preserve 
		keep if (gender== 0 | gender==2) | (gender==1 & (rank/N) < `k'/100)
		collapse (sum) q (count) n=q, by(gender)
		gen which = `k'
		gen type = "perfect foresight"
		reshape wide q n, i(which type) j(gender)

		sleep 100
		append using data\simulated_sf_nf.dta
		sleep 100
		save data\simulated_sf_nf.dta, replace 
	restore 
}

 
forvalues k=0(1) 100 {
	preserve 
		set seed `k'
		drawnorm j  
		gsort gender j 
		bysort gender: replace rank =_n
		keep if (gender== 0 | gender==2) | (gender==1 & (rank/N) < `k'/100)
		collapse (sum) q (count) n=q, by(gender)
		gen which = `k'
		gen type = "no predictability"
		reshape wide q n, i(which type) j(gender)
		sleep 100
		append using data\simulated_sf_nf.dta
		sleep 100
		save data\simulated_sf_nf.dta, replace 
	restore 
}

use   data\simulated_sf_nf.dta, clear 
	gen sf = q1/(q1 + q0 + q2)
	gen nf = n1/(n1 + n0 + n2)
	
	reg sf nf if type=="perfect foresight" & nf>.25
	reg sf nf if type=="no predictability" & nf>.25
 
 
 twoway (scatter sf  nf if type=="perfect foresight") (scatter sf  nf if type=="no predictability")  if nf>.1, scheme(lean2) legend(order(1 "perfect foresight" 2 "no predictability")) xtitle(female-authored share of books) ytitle(share of sales to female-authored books)
 
 
use   data\simulated_sf_nf.dta, clear 
	gen sf = q1/(q1 + q0 )
	gen nf = n1/(n1 + n0 )
	
	reg sf nf if type=="perfect foresight" & nf>.25
	reg sf nf if type=="no predictability" & nf>.25
 
 
 twoway (scatter sf  nf if type=="perfect foresight") (scatter sf  nf if type=="no predictability")  if nf>.1, scheme(lean2) legend(order(1 "perfect foresight" 2 "no predictability")) xtitle(female-authored share of books) ytitle(share of sales to female-authored books)